/*
 * Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.interactions.DiscordLocale;
import net.dv8tion.jda.api.interactions.commands.Command;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.*;
import net.dv8tion.jda.api.interactions.commands.localization.LocalizationFunction;
import net.dv8tion.jda.api.interactions.commands.localization.ResourceBundleLocalizationFunction;

import javax.security.auth.login.LoginException;

public class LocalizationExample {
    public static void main(String[] args) throws LoginException {
        String token = "BOT_TOKEN_HERE";

        // Pretty standard JDA instance creation
        JDA jda = JDABuilder.createLight(token).build();

        // This is the function that will retrieve the localization strings from the specified bundles
        // The localized strings will be injected when the commands are sent to Discord
        // You can see the strings being used in the "resources" folder, in "MyCommands_es_ES.properties" and
        // "MyCommands_fr.properties"
        // You can also provide a custom implementation of LocalizationFunction if you wish to
        // This would enable you for example to get localizations in other format, or, allow wildcards to save some
        // space / remove duplicated localizations
        LocalizationFunction localizationFunction = ResourceBundleLocalizationFunction.fromBundles(
                        "MyCommands", DiscordLocale.SPANISH, DiscordLocale.FRENCH)
                .build();

        // Your normal ban command, no need to do anything special here
        SlashCommandData slashCommandData = Commands.slash("ban", "Bans someone")
                // Sets the localization function to use
                .setLocalizationFunction(localizationFunction)
                .addSubcommandGroups(new SubcommandGroupData("member", "Bans a member")
                        .addSubcommands(
                                banSubcommand("perm", "Bans a member permanently"),
                                banSubcommand("temp", "Bans a member temporarily")));

        // Manual approach to localizing commands
        CommandData messageCommand = Commands.message("Show raw content")
                .setNameLocalization(DiscordLocale.SPANISH, "Mostrar el contenido en bruto")
                .setNameLocalization(DiscordLocale.FRENCH, "Afficher contenu brut");

        CommandData userCommand = Commands.user("Show avatar")
                .setNameLocalization(DiscordLocale.SPANISH, "Mostrar avatar")
                .setNameLocalization(DiscordLocale.FRENCH, "Afficher avatar");

        // Manual description localization would work the exact same

        jda.updateCommands()
                .addCommands(slashCommandData, messageCommand, userCommand)
                .queue();
    }

    private static SubcommandData banSubcommand(String name, String description) {
        return new SubcommandData(name, description)
                .addOptions(
                        new OptionData(OptionType.STRING, "user", "The user to ban"),
                        new OptionData(OptionType.INTEGER, "del_days", "The amount of days to delete messages")
                                .addChoices(
                                        new Command.Choice("1 Day", "1"),
                                        new Command.Choice("7 Days", "7"),
                                        new Command.Choice("14 Days", "14")));
    }
}
